/*
 * FileName: LikeTiemsService.java
 *
 */
package com.mrc.ak.showMan.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mrc.ak.showMan.dao.LikeTimesDao;
import com.mrc.ak.showMan.model.LikeTimes;
import com.mrc.core.base.BaseDao;
import com.mrc.core.base.BaseService;

/**
 * <p>
 * Description:  点赞设置
 * </p>
 *
 * @author Mrc
 * @version 4.1

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2017-1-19 下午1:58:04          Mrc        4.1         To create
 * </p>
 *
 * @since 
 * @see     
 */
@Service
@Transactional
public class LikeTiemsService extends BaseService<LikeTimes> {

    @Autowired
    private LikeTimesDao likeTimesDao;

    /* (non-Javadoc)
     * @see com.mrc.core.base.BaseService#getDao()
     */
    @Override
    protected BaseDao<LikeTimes> getDao() {
        return likeTimesDao;
    }

    /**
     * 保存
     * @param folder
     * @param file
     * @param likeTimes 顶
     * @param treadTimes 踩
     * @return
     */
    public LikeTimes save(String folder, String file, int likeTimes, int treadTimes) {
        LikeTimes lt = new LikeTimes();
        lt.setFolderName(folder);
        lt.setFileName(file);
        lt.setLikeTimes(likeTimes);
        lt.setTreadTimes(treadTimes);
        super.save(lt);
        return lt;
    }

    /**
     * 根据文件夹、文件名 查找
     * @param folder
     * @param file
     * @return
     */
    public LikeTimes findByFolderAndFile(String folder, String file) {
        LikeTimes lt = likeTimesDao.findByFolderAndFile(folder, file);
        //        //如果第一次，没有数据，先保存后返回
        //        if (lt == null) {
        //            lt = this.save(folder, file, 0, 0);
        //        }
        return lt;
    }

    /**
     * 点赞
     * @param folderName
     * @param fileName
     */
    public void pariseIt(String folderName, String fileName) {
        LikeTimes lt = this.findByFolderAndFile(folderName, fileName);
        if (lt == null) {
            lt = this.save(folderName, fileName, 1, 0);
            return;
        }

        //次数加一 更新保存
        lt.setLikeTimes(lt.getLikeTimes() + 1);
        this.update(lt);
    }

    /**
     * 踩
     * @param folderName
     * @param fileName
     */
    public void treadIt(String folderName, String fileName) {
        LikeTimes lt = this.findByFolderAndFile(folderName, fileName);
        if (lt == null) {
            lt = this.save(folderName, fileName, 0, 1);
            return;
        }

        //次数加一 更新保存
        lt.setTreadTimes(lt.getTreadTimes() + 1);
        this.update(lt);
    }

}
